package net.bezrabotnih.cabinet.client.activity;

import java.util.List;
import java.util.Set;

import net.bezrabotnih.cabinet.client.place.CreateJobPlace;
import net.bezrabotnih.cabinet.client.place.EditJobPlace;
import net.bezrabotnih.cabinet.client.place.ProviderJobsPlace;
import net.bezrabotnih.cabinet.client.request.CabinetRequestFactory;
import net.bezrabotnih.cabinet.client.ui.CreateEditJobView;
import net.bezrabotnih.cabinet.client.ui.CreateEditJobViewImpl;
import net.bezrabotnih.client.managed.request.JobProxy;
import net.bezrabotnih.client.managed.request.JobRequest;
import net.bezrabotnih.client.managed.request.JobTypeProxy;
import net.bezrabotnih.client.managed.request.UserAccountProxy;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.requestfactory.shared.Violation;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class EditJobActivity  extends AbstractActivity implements CreateEditJobView.Presenter{

	
	private EditJobPlace place;
	private CabinetRequestFactory requestFactory;
	private PlaceController placeController;
	private CreateEditJobView view;
	
	private JobProxy jobProxy;

	public EditJobActivity(EditJobPlace place,CabinetRequestFactory requestFactory,PlaceController placeController) {
	    this.place=place;
	    this.requestFactory=requestFactory;
	    this.placeController=placeController;
	
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		view = CreateEditJobViewImpl.instance();
		view.setPresenter(this);
		containerWidget.setWidget(view.asWidget());
		if (!view.isAllJobTypesListInited()){
			requestFactory.cabinetJobTypeRequest().findAllJobTypes().fire(new Receiver<List<JobTypeProxy>>() {
				@Override
				public void onSuccess(List<JobTypeProxy> jobTypeProxies) {
					view.initAllJobTypesList(jobTypeProxies);
					initForm();
				}
			});
			
		}else{
			view.resetForms();
			initForm();
			
		}
		
		
	}
	
	@SuppressWarnings("deprecation")
	private void initForm(){
		
		EditJobActivity.this.view.setEditView(true);
		
		requestFactory.cabinetJobRequest().findJob(place.getJobId()).fire(new Receiver<JobProxy>() {
			public void onSuccess(JobProxy jobProxy) {
				
				EditJobActivity.this.view.setDeadlineDate(jobProxy.getDeadLine());
				EditJobActivity.this.view.setDescription(jobProxy.getDescription());
				EditJobActivity.this.view.setHourly(jobProxy.getIsHourly());
				EditJobActivity.this.view.setJobType(jobProxy.getJobTypeId());
				EditJobActivity.this.view.setName(jobProxy.getName());
				EditJobActivity.this.view.setPrice(jobProxy.getPrice());
				EditJobActivity.this.view.setStartDate(jobProxy.getStartDate());
				
				
				
				EditJobActivity.this.jobProxy=jobProxy;
				
				
			};
		});
	}

	@Override
	public void goTo(Place place) {
		// TODO Auto-generated method stub
		
	}

	@SuppressWarnings("deprecation")
	@Override
	public void saveClicked() {

		if (jobProxy==null) return;
		
		final JobRequest jobRequest=requestFactory.jobRequest();
		jobProxy=jobRequest.edit(jobProxy);
		
//		jobProxy.setDeadLine(view.getDeadlineDate());
		jobProxy.setDescription(view.getDescription());
//		jobProxy.setIsHourly(view.isHourly());
//		jobProxy.setJobTypeId(view.getJobTypeId());
//		jobProxy.setName(view.getName());
//		jobProxy.setPrice(view.getPrice());
//		jobProxy.setStartDate(view.getStartDate());
		
		
		requestFactory.cabinetUserAccountRequest().currentUserAccount().fire(new Receiver<UserAccountProxy>() {
			@Override
			public void onSuccess(final UserAccountProxy userAccountProxy) {
				jobProxy.setProviderId(userAccountProxy.getId());
				jobRequest.persist().using(jobProxy).fire(new Receiver<Void>() {
					@Override
					public void onSuccess(Void response) {
						
					placeController.goTo(new ProviderJobsPlace());
					}
					@Override
					public void onViolation(Set<Violation> errors) {
						String errorStr="";
						for (Violation error:errors){
							errorStr+=error.getPath()+" "+error.getMessage()+", ";
						}
						Window.alert(errorStr);
					}
				});
			}
		});
		
		
		
	
		
	}

}
